package com.songcha.library_business.proxy.teenager

import com.songcha.library_common.helper.SharedPreferenceHelper
import com.songcha.library_common.util.TimeUtil

//Teenager mode
class TeenagerManager {
    companion object{
        var isTeenagerMode=false
        var teenagerPwd=""
        //max use in teenager mode,default is 40 minute
        const val MAX_USE_TIME=60*40
        //already use time
        var useTime=0
        @JvmField
        var lastUseTime=0

        @JvmStatic
        fun enterTeenagerMode(pwd:String){
            isTeenagerMode=true
            teenagerPwd=pwd
            SharedPreferenceHelper.setBoolean("teenager","is_teenager_mode",true)
            SharedPreferenceHelper.setString("teenager","teenager_pwd",pwd)
        }

        @JvmStatic
        fun exitTeenagerMode(){
            isTeenagerMode=false
            teenagerPwd=""
            SharedPreferenceHelper.setBoolean("teenager","is_teenager_mode",false)
            SharedPreferenceHelper.setString("teenager","teenager_pwd","")
        }

        @JvmStatic
        fun addUseTime(second:Int){
            useTime+=second
            SharedPreferenceHelper.setInt("teenager","use_time", useTime)
        }

        @JvmStatic
        fun setLastUseTime(){
            lastUseTime=TimeUtil.getCurrentTimeSecond().toInt()
            SharedPreferenceHelper.setInt("teenager","last_use_time", lastUseTime)
        }

        @JvmStatic
        fun getLastUseTime():Int{
            return lastUseTime
        }


        @JvmStatic
        fun resetUseTime(){
            useTime=0
            lastUseTime=0
            SharedPreferenceHelper.setInt("teenager","use_time", 0)
            SharedPreferenceHelper.setInt("teenager","last_use_time", -1)
        }


        @JvmStatic
        fun init(){
            teenagerPwd=SharedPreferenceHelper.getString("teenager","teenager_pwd")
            isTeenagerMode=teenagerPwd.isNotBlank() && SharedPreferenceHelper.getBoolean("teenager","is_teenager_mode")
            useTime=SharedPreferenceHelper.getInt("teenager","use_time")
            lastUseTime=SharedPreferenceHelper.getInt("teenager","last_use_time")
            if(lastUseTime==-1){
               setLastUseTime()
            }
        }

    }
}